package com.ims.manager.factory;

import com.ims.common.constant.Constants;
import com.ims.common.utils.LogUtils;
import com.ims.common.utils.ServletUtils;
import com.ims.common.utils.StringUtils;
import com.ims.common.utils.ip.AddressUtils;
import com.ims.common.utils.ip.IpUtils;
import com.ims.common.utils.spring.SpringUtils;
import com.ims.core.doc.service.ITInCollectService;
import com.ims.core.doc.service.ITInDeductionStatService;
import com.ims.core.doc.service.ITInInvoiceService;
import com.ims.core.doc.service.ITSalInvoiceService;
import com.ims.core.doc.vo.bwapi.InvoiceForValidate;
import com.ims.system.domain.SysLogininfor;
import com.ims.system.domain.SysOperLog;
import com.ims.system.service.ISysLogininforService;
import com.ims.system.service.ISysOperLogService;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author IMS
 */
public class ImsAsyncFactory
{
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 同步发票信息（百望查询）
     * @param taxNo
     * @param applyNo
     * @param invoiceNo
     * @return
     */
    public static TimerTask syncInvoiceFromBaiwang(final String taxNo, final String applyNo, final String invoiceNo)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITSalInvoiceService.class).queryAndSaveInvoiceByInvoiceNo(taxNo, applyNo, invoiceNo);
            }
        };
    }

    /**
     * 延迟同步发票信息（百望查询）
     * @param taxNo
     * @param applyNo
     * @param invoiceNo
     * @return
     */
    public static TimerTask syncInvoiceFromBaiwangDelay(final String taxNo, final String applyNo, final String invoiceNo)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                try {
                    Thread.sleep(3000); // 暂停1秒钟
                    SpringUtils.getBean(ITSalInvoiceService.class).queryAndSaveInvoiceByInvoiceNo(taxNo, applyNo, invoiceNo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }

    /**
     * 发票版式生成请求（百望生成）
     * @param taxNo
     * @param invoiceNo
     * @return
     */
    public static TimerTask invoiceFormatCreateFromBaiwang(final String taxNo, final String invoiceNo)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITSalInvoiceService.class).formatCreateByInvoiceNo(taxNo, invoiceNo);
            }
        };
    }

    /**
     * 查验发票
     * @param collectId 采集单ID
     * @param invoiceForValidateList 查验数据集合
     * @return
     */
    public static TimerTask invoiceValidateFromBaiwang(final String collectId, final List<InvoiceForValidate> invoiceForValidateList)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInCollectService.class).invoiceValidate(collectId, invoiceForValidateList);
            }
        };
    }

    /**
     * 1. 获取 勾选抵扣 申请结果
     * @param deductionId 勾选申请ID
     * @return
     */
    public static TimerTask syncDeductionApplyResultFromBaiwang(final String deductionId)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInInvoiceService.class).deductionApplyResult(deductionId);
            }
        };
    }

    /**
     * 2. 获取取消- 勾选抵扣 申请结果
     * @param deductionId 勾选申请ID
     * @return
     */
    public static TimerTask syncDeductionApplyCancelResultFromBaiwang(final String deductionId)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInInvoiceService.class).deductionApplyCancelResult(deductionId);
            }
        };
    }

    /**
     * 3. 获取 勾选不抵扣 申请结果
     * @param deductionId 勾选申请ID
     * @return
     */
    public static TimerTask syncDeductionCancelnonApplyResultFromBaiwang(final String deductionId)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInInvoiceService.class).deductionCancelnonApplyResult(deductionId);
            }
        };
    }

    /**
     * 4. 获取取消- 勾选不抵扣 申请结果
     * @param deductionId 勾选申请ID
     * @return
     */
    public static TimerTask syncDeductionCancelnonApplyCancelResultFromBaiwang(final String deductionId)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInInvoiceService.class).deductionCancelnonApplyCancelResult(deductionId);
            }
        };
    }


    /**
     * 统计表生成申请
     * @param statId 统计表ID
     * @return
     */
    public static TimerTask deductionStatApplyFromBaiwang(final String statId)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtils.getBean(ITInDeductionStatService.class).applyConfirmTInDeductionStat(statId);
            }
        };
    }


}
